<template>
  <div>
    <el-card style="width: 100%" v-loading="loading">
      <el-card>
        <el-button type="primary" size="small" @click="handleAdd(1)">添加链接({{ postData.connectors.length }})</el-button>
        <div class="list-sql">
          <div class="list-sql-item-hover" v-for="(item, index) in postData.connectors" :key="index"
            @click="handleLinkDetails(item.id)">
            <div class="list-sql-item">
              <img src="../../assets/logo.png" />
              <div class="list-sql-item-text">{{ item.name }}</div>

              <el-popover placement="top-start" width="150" trigger="hover">
                <div>
                  <el-button type="primary" size="mini" @click="handleCopyConnector(item.id)">复制</el-button>
                  <el-button type="danger" size="mini" @click="handleDelConnector(item.id)">删除</el-button>
                </div>
                <i class="el-icon-share" slot="reference"></i>
              </el-popover>
            </div>
          </div>
        </div>
      </el-card>
      <div style="height: 30px"></div>
      <el-card>
        <el-button type="primary" size="small" @click="handleAdd(2)">添加驱动({{ postData.mappings.length }})</el-button>
        <div class="list-sql">
          <div class="list-sql-item-hover special" @click="handleDriveDetails(item.id)"
            v-for="(item, index) in postData.mappings" :key="index">
            <div class="list-sql-item special">
              <!-- <img src="../../assets/logo.png" /> -->
              <div class="list-sql-item-text">{{ item.name }}</div>
              <div class="start-end">
                <div class="start">
                  <img src="../../assets/logo.png" />
                  <span>{{ item.sourceConnector.name }}</span>
                </div>
                <div class="hr">
                  <div class="state">{{ item.meta.state | metaState }}</div>
                </div>
                <div class="end">
                  <img src="../../assets/logo.png" />
                  <span>{{ item.targetConnector.name }}</span>
                </div>
              </div>
              <div class="ul">
                <div class="li">{{ item.meta.model }}同步>总数:0</div>
                <div class="li">启动时间>{{ item.updateTime | YYYY_MM_DD_HH_MM_SS }}</div>
              </div>
              <div class="time">{{ item.createTime | YYYY_MM_DD_HH_MM_SS }}</div>

              <el-popover placement="top-start" width="200" trigger="hover">
                <div>
                  <el-button type="success" size="mini" v-if="Number(item.meta.state) == 0"
                    @click="handleStart(item.id)">启动</el-button>
                  <el-button type="error" size="mini" v-if="Number(item.meta.state) == 1"
                    @click="handleStop(item.id)">停止</el-button>
                  <el-button type="primary" size="mini" @click="handleCopyDrive(item.id)">复制</el-button>
                  <el-button type="danger" size="mini" @click="handleDelDrive(item.id)">删除</el-button>
                </div>
                <i class="el-icon-share" slot="reference"></i>
              </el-popover>
            </div>
          </div>
        </div>
      </el-card>
    </el-card>
    <drive-add @success="getList" ref="drive-add"></drive-add>
    <link-add @success="getList" ref="link-add"></link-add>
    <drive-edit @success="getList" ref="drive-edit"></drive-edit>
  </div>
</template>

<script>
import DriveAdd from "./components/drive-add.vue";
import LinkAdd from "./components/link-add.vue";
import DriveEdit from "./components/drive-edit.vue";

let deleteInformation = () => { };
import {
  listIndex, copyMapping, delMapping,
  copyConnector, delConnector, startMapping,
  stopMapping
} from "@/api/drive/index";
export default {
  components: {
    DriveAdd,
    LinkAdd,
    DriveEdit
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      postData: {},
      // 弹出层标题
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
    };
  },
  async created() {
    this.getList();
  },
  methods: {
    /** 查询岗位列表 */
    async getList() {
      try {
        this.loading = true;
        let res = await listIndex();
        this.postData = res.resultValue
        this.loading = false;
      } catch (e) {
        this.loading = false;
      }
    },
    async handleDelConnector(id) {
      try {
        await this.$confirm('确认删除连接?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        });
        this.loading = true;
        await delConnector({ id });
        this.getList();
      } catch (error) {
        this.loading = false;
      }
    },
    handleLinkDetails(id) {
      this.$refs['link-add'].init(id);
    },
    handleDriveDetails(id) {
      this.$refs['drive-edit'].init(id);
    },
    async handleCopyConnector(id) {
      try {
        this.loading = true;
        await copyConnector({ id });
        this.getList();
      } catch (error) {
        this.loading = false;
      }
    },
    async handleStart(id) {
      try {
        this.loading = true;
        await startMapping({}, { id });
        this.$message.success("启动成功")
        this.getList();
      } catch (error) {
        this.loading = false;
      }
    },
    async handleStop(id) {
      try {
        this.loading = true;
        await startMapping({}, { id });
        this.$message.success("停用成功")
        this.getList();
      } catch (error) {
        this.loading = false;
      }
    },
    async handleDelDrive(id) {
      try {
        await this.$confirm('确认删除驱动?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        });
        this.loading = true;
        await delMapping({ id });
        this.getList();
      } catch (error) {
        this.loading = false;
      }
    },
    async handleCopyDrive(id) {
      try {
        this.loading = true;
        await copyMapping({ id });
        this.getList();
      } catch (error) {
        this.loading = false;
      }
    },
    /** 新增 */
    handleAdd(state) {
      if (state == 2) {
        this.$refs["drive-add"].init();
      }
      if (state == 1) {
        this.$refs["link-add"].init();
      }
    },
    /** 编辑 */
    handleEdit(row) {
      if (this.ids.length != 1 && !row.id)
        return this.$message.error("请选择一个数据进行编辑");
      let postIds = row.id || this.ids[0];
      this.$refs["drive-add"].init(postIds);
    },
    /** 删除 */
    handleDel(row) {
      let postIds = row.id || this.ids;
      if (this.ids.length != 1 && !row.id)
        return this.$message.error("请选择要删除的数据");
      if (Array.isArray(postIds)) postIds = postIds.toString();
      this.$confirm("确认删除用户吗？删除后，数据不能恢复。", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return deleteInformation(postIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs["queryForm"].resetFields();
      this.handleQuery();
    },
    /** 导出按钮操作 */
    async handleExport() {
      this.download(
        "/lgb/purchaseHistory/export",
        {
          ...this.queryParams,
        },
        `老干部消费_${new Date().getTime()}.xlsx`
      );
    },
  },
  filters: {
    metaState(n) {
      switch (Number(n)) {
        case 0:
          return "未运行";
        case 1:
          return "运行中";
        case 2:
          return "停止中";
        default:
          return "";
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.list-sql {
  display: flex;
  flex-wrap: wrap;
  font-size: 12px;

  .list-sql-item-hover {
    width: 100px;
    height: 100px;
    margin-right: 20px;
    margin-top: 20px;
  }

  .list-sql-item {
    position: relative;
    cursor: pointer;
    border-radius: 8px;
    border: 1px solid #cccccc;
    width: 100px;
    height: 100px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    transition: all 0.3s;

    .list-sql-item-text {
      color: #666666;
      margin-top: 4px;
      font-size: 14px;
    }

    .el-icon-share {
      position: absolute;
      right: 4px;
      top: 4px;
      font-size: 14px;
    }

    img {
      width: 60px;
      height: 60px;
    }

    .time {
      position: absolute;
      right: 4px;
      bottom: 10px;
      color: #666666;
    }
  }

  .list-sql-item-hover:hover .list-sql-item {
    transform: scale(1.05);
  }

  .list-sql-item-hover.special,
  .list-sql-item.special {
    width: 350px;
    height: 150px;
  }

  .list-sql-item-hover.special {
    margin-right: 40px;
    margin-top: 40px;
  }

  .start-end {
    display: flex;
    align-items: center;

    .start,
    .end {
      display: flex;
      align-items: center;
      border-radius: 8px;
      padding: 4px 8px;
      border: 1px solid #cccccc;
      margin: 8px 0;
      width: 100px;

      img {
        width: 40px;
        height: 40px;
      }

      span {
        margin-left: 4px;
      }
    }

    .hr {
      width: 80px;
      height: 0px;
      border-top: 1px solid #cccccc;
      position: relative;

      .state {
        position: absolute;
        color: #FFFFFF;
        background-color: #5bc0de;
        padding: 2px 6px;
        // font-weight: bold;
        white-space: nowrap;
        border-radius: 4px;
        font-size: 10px;
        bottom: -22px;
        left: 50%;
        transform: translate(-50%, 0);
      }
    }
  }

  .ul {
    width: 100%;

    .li {
      padding: 0 20px;
      margin-bottom: 4px;
    }
  }
}
</style>
